package br.indt.framework.ui.common;

import javax.microedition.lcdui.Graphics;

import br.indt.framework.ui.ImageLoader;
import br.indt.framework.utils.StringUtils;


public class TitleBarFullTouch extends TitleBar
{
	protected static final String LEFT_SOFTKEY_ITEM = "topbar_menu_a.png";
	protected static final String LEFT_SOFTKEY_ITEM_PRESS = "topbar_menu_b.png";
	protected static final String MIDDLE_SOFTKEY_ITEM = "topo_busca_02.png";
	protected static final String MIDDLE_SOFTKEY_ITEM_PRESS = "topo_busca_01.png";

	private MenuItem lskItem = null;
	private MenuItem mskItem = null;
	private MenuItemListener menuItemListener = null;
	private Icon iconLSK = null;
	private Icon iconMSK = null;


	public TitleBarFullTouch(String name, String title)
	{
		super(name, title);

		iconLSK = ImageLoader.createIcon("iconLSK", LEFT_SOFTKEY_ITEM, LEFT_SOFTKEY_ITEM_PRESS, -1);
		iconMSK = ImageLoader.createIcon("iconMSK", MIDDLE_SOFTKEY_ITEM, MIDDLE_SOFTKEY_ITEM_PRESS, -1);

		int val = 0;
		
		if (iconLSK != null)
		{
			val++;
			lskItem = new MenuItem(StringUtils.EMPTY);
        }

		if (iconMSK != null)
		{

			val++;
			mskItem =  new MenuItem(StringUtils.EMPTY);
        }
		
		titleLabelLeft = 40;
		
		try 
		{
			titleLabel.setWidth(Display.getInstance().getWidth() - (val * titleLabelLeft));
		}
		catch (Exception e) 
		{
		//	Utils.logError("TitleBarFullTouch::constructor - e = " + e);
		}
	}

	public void pressFire()
	{
		// empty
	}

	public void paint(Graphics g, int x, int y, int maxWidth, int maxHeight)
	{
		super.paint(g, x, y, maxWidth, maxHeight);

		try
		{
			if (iconLSK != null)
			{
				iconLSK.paint(g, x+7, 12, maxWidth, maxHeight);//x,7
			}

			if (iconMSK != null)
			{					
				iconMSK.paint(g, Display.getInstance().getWidth() - 32, 12, maxWidth, maxHeight);
			}
		}
		catch (Exception e) {
			//empty
		}
	}

	public void setMenuItemListener(MenuItemListener menuItemListener)
	{
		this.menuItemListener = menuItemListener;
	}

	public MenuItem getLskItem()
	{
		return lskItem;
	}

	public void setLskItem(MenuItem lskItem)
	{
		this.lskItem = lskItem;
	}

	public MenuItem getMskItem()
	{
		return mskItem;
	}

	public void setMskItem(MenuItem mskItem)
	{
		this.mskItem = mskItem;
	}

	public Icon getIconLSK() 
	{
		return iconLSK;
	}

	public void setIconLSK(Icon iconLSK) 
	{
		this.iconLSK = iconLSK;
		
		if(iconLSK == null){
			titleLabelLeft = 6;
		}
	}

	public void setIconMSK(Icon iconRSK) 
	{
		this.iconMSK = iconRSK;
	}
	
	public Icon getIconMSK() {
		return iconMSK;
	}

	public void click(int x, int y) 
	{
		
		System.out.println("click title bar x:"+x);
		try 
		{
			Display display = Display.getInstance();

			if (x < 42 && iconLSK != null)
			{
				System.out.println("TitleBar::click LSK clicked");

				if (menuItemListener != null) 
				{
					menuItemListener.menuItemClicked(lskItem);
				}
				iconLSK.setPressedDownState(false);
			}
			else if (x > display.getWidth()- 32 && iconMSK != null)
			{
				System.out.println("TitleBar::click MSK clicked"+menuItemListener);
				
				if (menuItemListener != null)
				{
					menuItemListener.menuItemClicked(mskItem);
				}
				iconMSK.setPressedDownState(false);
			}
		}
		catch (Exception e)
		{
			System.out.println("[ERROR] TitleBar::click - e = " + e);
		}
	}
}